Add remaining files
[juce-lv2.git] / juce / source / extras / the jucer / src / model / components / jucer_LabelHandler.h
blob8f856779952c9f4852c1ad1e0c61280ded34a8d3
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_LABELHANDLER_JUCEHEADER__
27 #define __JUCER_LABELHANDLER_JUCEHEADER__
29 #include "../../properties/jucer_JustificationProperty.h"
30 #include "../../properties/jucer_FontPropertyComponent.h"
31 #include "../../properties/jucer_ComponentBooleanProperty.h"
34 //==============================================================================
35 /**
37 class LabelHandler : public ComponentTypeHandler
39 public:
40 //==============================================================================
41 LabelHandler()
42 : ComponentTypeHandler ("Label", "Label", typeid (Label), 150, 24)
44 registerColour (Label::backgroundColourId, "background", "bkgCol");
45 registerColour (Label::textColourId, "text", "textCol");
46 registerColour (Label::outlineColourId, "outline", "outlineCol");
47 registerColour (TextEditor::textColourId, "editor text", "edTextCol");
48 registerColour (TextEditor::backgroundColourId, "editor bkg", "edBkgCol");
49 registerColour (TextEditor::highlightColourId, "highlight", "hiliteCol");
52 //==============================================================================
53 Component* createNewComponent (JucerDocument*)
55 return new Label ("new label", "label text");
58 XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
60 Label* const l = dynamic_cast <Label*> (comp);
62 XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
63 e->setAttribute ("labelText", l->getText());
65 e->setAttribute ("editableSingleClick", l->isEditableOnSingleClick());
66 e->setAttribute ("editableDoubleClick", l->isEditableOnDoubleClick());
67 e->setAttribute ("focusDiscardsChanges", l->doesLossOfFocusDiscardChanges());
69 e->setAttribute ("fontname", l->getProperties().getWithDefault ("typefaceName", FontPropertyComponent::defaultFont).toString());
70 e->setAttribute ("fontsize", roundToInt (l->getFont().getHeight() * 100.0) / 100.0);
71 e->setAttribute ("bold", l->getFont().isBold());
72 e->setAttribute ("italic", l->getFont().isItalic());
73 e->setAttribute ("justification", l->getJustificationType().getFlags());
75 return e;
78 bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
80 Label* const l = dynamic_cast <Label*> (comp);
82 if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
83 return false;
85 Label defaultLabel (String::empty, String::empty);
87 Font font;
88 font.setHeight ((float) xml.getDoubleAttribute ("fontsize", 15.0));
89 font.setBold (xml.getBoolAttribute ("bold", false));
90 font.setItalic (xml.getBoolAttribute ("italic", false));
91 l->setFont (font);
93 l->getProperties().set ("typefaceName", xml.getStringAttribute ("fontname", FontPropertyComponent::defaultFont));
94 updateLabelFont (l);
96 l->setJustificationType (Justification (xml.getIntAttribute ("justification", Justification::centred)));
98 l->setText (xml.getStringAttribute ("labelText", "Label Text"), false);
100 l->setEditable (xml.getBoolAttribute ("editableSingleClick", defaultLabel.isEditableOnSingleClick()),
101 xml.getBoolAttribute ("editableDoubleClick", defaultLabel.isEditableOnDoubleClick()),
102 xml.getBoolAttribute ("focusDiscardsChanges", defaultLabel.doesLossOfFocusDiscardChanges()));
104 return true;
107 static void updateLabelFont (Label* label)
109 Font f (label->getFont());
110 f = FontPropertyComponent::applyNameToFont (label->getProperties().getWithDefault ("typefaceName", FontPropertyComponent::defaultFont), f);
111 label->setFont (f);
114 const String getCreationParameters (Component* component)
116 Label* const l = dynamic_cast <Label*> (component);
118 return quotedString (component->getName())
119 + ",\n"
120 + quotedString (l->getText());
123 void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
125 ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
127 Label* const l = dynamic_cast <Label*> (component);
129 String s;
131 s << memberVariableName << "->setFont ("
132 << FontPropertyComponent::getCompleteFontCode (l->getFont(), l->getProperties().getWithDefault ("typefaceName", FontPropertyComponent::defaultFont))
133 << ");\n"
134 << memberVariableName << "->setJustificationType ("
135 << justificationToCode (l->getJustificationType())
136 << ");\n"
137 << memberVariableName << "->setEditable ("
138 << boolToString (l->isEditableOnSingleClick()) << ", "
139 << boolToString (l->isEditableOnDoubleClick()) << ", "
140 << boolToString (l->doesLossOfFocusDiscardChanges()) << ");\n"
141 << getColourIntialisationCode (component, memberVariableName);
143 if (needsCallback (component))
144 s << memberVariableName << "->addListener (this);\n";
146 s << '\n';
148 code.constructorCode += s;
151 void fillInGeneratedCode (Component* component, GeneratedCode& code)
153 ComponentTypeHandler::fillInGeneratedCode (component, code);
155 if (needsCallback (component))
157 String& callback = code.getCallbackCode ("public LabelListener",
158 "void",
159 "labelTextChanged (Label* labelThatHasChanged)",
160 true);
162 if (callback.trim().isNotEmpty())
163 callback << "else ";
165 const String memberVariableName (code.document->getComponentLayout()->getComponentMemberVariableName (component));
166 const String userCodeComment ("UserLabelCode_" + memberVariableName);
168 callback
169 << "if (labelThatHasChanged == " << memberVariableName
170 << ")\n{\n //[" << userCodeComment << "] -- add your label text handling code here..\n //[/" << userCodeComment << "]\n}\n";
174 void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
176 ComponentTypeHandler::getEditableProperties (component, document, properties);
178 Label* const l = dynamic_cast <Label*> (component);
179 properties.add (new LabelTextProperty (l, document));
181 properties.add (new LabelJustificationProperty (l, document));
182 properties.add (new FontNameProperty (l, document));
183 properties.add (new FontSizeProperty (l, document));
184 properties.add (new FontStyleProperty (l, document));
186 addColourProperties (component, document, properties);
188 properties.add (new LabelEditableProperty (l, document));
190 if (l->isEditableOnDoubleClick() || l->isEditableOnSingleClick())
191 properties.add (new LabelLossOfFocusProperty (l, document));
194 static bool needsCallback (Component* label)
196 return ((Label*) label)->isEditableOnSingleClick()
197 || ((Label*) label)->isEditableOnDoubleClick(); // xxx should be configurable
200 private:
202 //==============================================================================
203 class LabelTextProperty : public ComponentTextProperty <Label>
205 public:
206 LabelTextProperty (Label* component_, JucerDocument& document_)
207 : ComponentTextProperty <Label> ("text", 10000, true, component_, document_)
210 void setText (const String& newText)
212 document.perform (new LabelTextChangeAction (component, *document.getComponentLayout(), newText),
213 "Change Label text");
216 const String getText() const
218 return component->getText();
221 private:
222 class LabelTextChangeAction : public ComponentUndoableAction <Label>
224 public:
225 LabelTextChangeAction (Label* const comp, ComponentLayout& layout, const String& newState_)
226 : ComponentUndoableAction <Label> (comp, layout),
227 newState (newState_)
229 oldState = comp->getText();
232 bool perform()
234 showCorrectTab();
235 getComponent()->setText (newState, false);
236 changed();
237 return true;
240 bool undo()
242 showCorrectTab();
243 getComponent()->setText (oldState, false);
244 changed();
245 return true;
248 String newState, oldState;
252 //==============================================================================
253 class LabelEditableProperty : public ComponentChoiceProperty <Label>
255 public:
256 LabelEditableProperty (Label* component_,
257 JucerDocument& document_)
258 : ComponentChoiceProperty <Label> ("editing", component_, document_)
260 choices.add ("read-only");
261 choices.add ("edit on single-click");
262 choices.add ("edit on double-click");
265 void setIndex (int newIndex)
267 document.perform (new LabelEditableChangeAction (component, *document.getComponentLayout(), newIndex),
268 "Change Label editability");
271 int getIndex() const
273 return component->isEditableOnSingleClick()
275 : (component->isEditableOnDoubleClick() ? 2 : 0);
278 private:
279 class LabelEditableChangeAction : public ComponentUndoableAction <Label>
281 public:
282 LabelEditableChangeAction (Label* const comp, ComponentLayout& layout, const int newState_)
283 : ComponentUndoableAction <Label> (comp, layout),
284 newState (newState_)
286 oldState = comp->isEditableOnSingleClick()
288 : (comp->isEditableOnDoubleClick() ? 2 : 0);
291 bool perform()
293 showCorrectTab();
294 getComponent()->setEditable (newState == 1, newState >= 1, getComponent()->doesLossOfFocusDiscardChanges());
295 changed();
296 layout.getSelectedSet().changed();
297 return true;
300 bool undo()
302 showCorrectTab();
303 getComponent()->setEditable (oldState == 1, oldState >= 1, getComponent()->doesLossOfFocusDiscardChanges());
304 changed();
305 layout.getSelectedSet().changed();
306 return true;
309 int newState, oldState;
313 //==============================================================================
314 class LabelLossOfFocusProperty : public ComponentChoiceProperty <Label>
316 public:
317 LabelLossOfFocusProperty (Label* component_,
318 JucerDocument& document_)
319 : ComponentChoiceProperty <Label> ("focus", component_, document_)
321 choices.add ("loss of focus discards changes");
322 choices.add ("loss of focus commits changes");
325 void setIndex (int newIndex)
327 document.perform (new LabelFocusLossChangeAction (component, *document.getComponentLayout(), newIndex == 0),
328 "Change Label focus behaviour");
331 int getIndex() const
333 return component->doesLossOfFocusDiscardChanges() ? 0 : 1;
336 private:
337 class LabelFocusLossChangeAction : public ComponentUndoableAction <Label>
339 public:
340 LabelFocusLossChangeAction (Label* const comp, ComponentLayout& layout, const bool newState_)
341 : ComponentUndoableAction <Label> (comp, layout),
342 newState (newState_)
344 oldState = comp->doesLossOfFocusDiscardChanges();
347 bool perform()
349 showCorrectTab();
350 getComponent()->setEditable (getComponent()->isEditableOnSingleClick(),
351 getComponent()->isEditableOnDoubleClick(),
352 newState);
353 changed();
354 return true;
357 bool undo()
359 showCorrectTab();
360 getComponent()->setEditable (getComponent()->isEditableOnSingleClick(),
361 getComponent()->isEditableOnDoubleClick(),
362 oldState);
363 changed();
364 return true;
367 bool newState, oldState;
371 //==============================================================================
372 class LabelJustificationProperty : public JustificationProperty,
373 public ChangeListener
375 public:
376 LabelJustificationProperty (Label* const label_, JucerDocument& document_)
377 : JustificationProperty ("layout", false),
378 label (label_),
379 document (document_)
381 document.addChangeListener (this);
384 ~LabelJustificationProperty()
386 document.removeChangeListener (this);
389 void setJustification (const Justification& newJustification)
391 document.perform (new LabelJustifyChangeAction (label, *document.getComponentLayout(), newJustification),
392 "Change Label justification");
395 const Justification getJustification() const
397 return label->getJustificationType();
400 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
402 private:
403 Label* const label;
404 JucerDocument& document;
406 class LabelJustifyChangeAction : public ComponentUndoableAction <Label>
408 public:
409 LabelJustifyChangeAction (Label* const comp, ComponentLayout& layout, const Justification& newState_)
410 : ComponentUndoableAction <Label> (comp, layout),
411 newState (newState_),
412 oldState (comp->getJustificationType())
416 bool perform()
418 showCorrectTab();
419 getComponent()->setJustificationType (newState);
420 changed();
421 return true;
424 bool undo()
426 showCorrectTab();
427 getComponent()->setJustificationType (oldState);
428 changed();
429 return true;
432 Justification newState, oldState;
436 //==============================================================================
437 class FontNameProperty : public FontPropertyComponent,
438 public ChangeListener
440 public:
441 FontNameProperty (Label* const label_,
442 JucerDocument& document_)
443 : FontPropertyComponent ("font"),
444 label (label_),
445 document (document_)
447 document.addChangeListener (this);
450 ~FontNameProperty()
452 document.removeChangeListener (this);
455 void setTypefaceName (const String& newFontName)
457 document.perform (new FontNameChangeAction (label, *document.getComponentLayout(), newFontName),
458 "Change Label typeface");
461 const String getTypefaceName() const
463 return label->getProperties().getWithDefault ("typefaceName", FontPropertyComponent::defaultFont);
466 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
468 private:
469 Label* const label;
470 JucerDocument& document;
472 class FontNameChangeAction : public ComponentUndoableAction <Label>
474 public:
475 FontNameChangeAction (Label* const comp, ComponentLayout& layout, const String& newState_)
476 : ComponentUndoableAction <Label> (comp, layout),
477 newState (newState_)
479 oldState = comp->getProperties().getWithDefault ("typefaceName", FontPropertyComponent::defaultFont);
482 bool perform()
484 showCorrectTab();
485 getComponent()->getProperties().set ("typefaceName", newState);
486 LabelHandler::updateLabelFont (getComponent());
487 changed();
488 return true;
491 bool undo()
493 showCorrectTab();
494 getComponent()->getProperties().set ("typefaceName", oldState);
495 LabelHandler::updateLabelFont (getComponent());
496 changed();
497 return true;
500 String newState, oldState;
504 //==============================================================================
505 class FontSizeProperty : public SliderPropertyComponent,
506 public ChangeListener
508 public:
509 FontSizeProperty (Label* const label_, JucerDocument& document_)
510 : SliderPropertyComponent ("size", 1.0, 250.0, 0.1, 0.3),
511 label (label_),
512 document (document_)
514 document.addChangeListener (this);
517 ~FontSizeProperty()
519 document.removeChangeListener (this);
522 void setValue (double newValue)
524 document.getUndoManager().undoCurrentTransactionOnly();
526 document.perform (new FontSizeChangeAction (label, *document.getComponentLayout(), (float) newValue),
527 "Change Label font size");
530 double getValue() const
532 return label->getFont().getHeight();
535 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
537 private:
538 Label* const label;
539 JucerDocument& document;
541 class FontSizeChangeAction : public ComponentUndoableAction <Label>
543 public:
544 FontSizeChangeAction (Label* const comp, ComponentLayout& layout, const float newState_)
545 : ComponentUndoableAction <Label> (comp, layout),
546 newState (newState_)
548 oldState = comp->getFont().getHeight();
551 bool perform()
553 showCorrectTab();
554 Font f (getComponent()->getFont());
555 f.setHeight ((float) newState);
556 getComponent()->setFont (f);
557 changed();
558 return true;
561 bool undo()
563 showCorrectTab();
564 Font f (getComponent()->getFont());
565 f.setHeight ((float) oldState);
566 getComponent()->setFont (f);
567 changed();
568 return true;
571 float newState, oldState;
575 //==============================================================================
576 class FontStyleProperty : public ChoicePropertyComponent,
577 public ChangeListener
579 public:
580 FontStyleProperty (Label* const label_, JucerDocument& document_)
581 : ChoicePropertyComponent ("style"),
582 label (label_),
583 document (document_)
585 document.addChangeListener (this);
587 choices.add ("normal");
588 choices.add ("bold");
589 choices.add ("italic");
590 choices.add ("bold + italic");
593 ~FontStyleProperty()
595 document.removeChangeListener (this);
598 void setIndex (int newIndex)
600 Font f (label->getFont());
602 f.setBold (newIndex == 1 || newIndex == 3);
603 f.setItalic (newIndex == 2 || newIndex == 3);
605 document.perform (new FontStyleChangeAction (label, *document.getComponentLayout(), f),
606 "Change Label font style");
609 int getIndex() const
611 if (label->getFont().isBold() && label->getFont().isItalic())
612 return 3;
613 else if (label->getFont().isBold())
614 return 1;
615 else if (label->getFont().isItalic())
616 return 2;
618 return 0;
621 void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
623 private:
624 Label* const label;
625 JucerDocument& document;
627 class FontStyleChangeAction : public ComponentUndoableAction <Label>
629 public:
630 FontStyleChangeAction (Label* const comp, ComponentLayout& layout, const Font& newState_)
631 : ComponentUndoableAction <Label> (comp, layout),
632 newState (newState_)
634 oldState = comp->getFont();
637 bool perform()
639 showCorrectTab();
640 getComponent()->setFont (newState);
641 changed();
642 return true;
645 bool undo()
647 showCorrectTab();
648 getComponent()->setFont (oldState);
649 changed();
650 return true;
653 Font newState, oldState;
659 #endif // __JUCER_LABELHANDLER_JUCEHEADER__